########## views.py ##############

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext, loader
from django.template.context import Context

from forms import *
from contact import *
from django.conf import settings
from django.contrib.admin.views.decorators import staff_member_required

def contactview(request, to_mail):
    if request.method == 'POST':
        data = {}
        for k,v in request.POST.items():
            data[k] = v
        if not request.user.is_anonymous():
            data['nombre'] = request.user
            data['mail'] = request.user.email
        form = EnviarComentario(data=data)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.save()
            #CASO: contactar al administrador.
            if to_mail:
                c = {
                    'username': data['nombre'],
                    'mail': data['mail'],
                    'title': settings.TITLE,
                    'texto': data['texto']
                }
                send_mail_html(to_mail, data['asunto'], 'email_comentario.html', c)
                return HttpResponseRedirect('/%s/notifications/thankyou/' % settings.PROJECT_NAME)
            #CASO: sugerir pag a un amigo.
            else:
                c = {
                    'username': data['nombre'],
                    'mail': data['mail'],
                    'title': settings.TITLE,
                    'url': settings.URL
                }
                send_mail_html(data['mail'], data['asunto'], 'email_sugerir_pag.html', c)
                return HttpResponseRedirect('/%s/'%settings.PROJECT_NAME)
        else:
            contact_to_admin = False
            if to_mail:
                contact_to_admin = True
            try:
                return render_to_response('custom/contacts.html',locals(),context_instance=RequestContext(request))
            except:
                return render_to_response('contacts.html',locals(),context_instance=RequestContext(request))
    else:
        form = EnviarComentario()
        contact_to_admin = False
        if to_mail:
            contact_to_admin = True
        try:
            return render_to_response('custom/contacts.html',locals(),context_instance=RequestContext(request))
        except:
            return render_to_response('contacts.html',locals(),context_instance=RequestContext(request))
    

def thankyou(request):
    return render_to_response('thankyou.html', {'current_tab': "contact"},context_instance=RequestContext(request))


@staff_member_required
def select_communication(request):
    list_communication = Communication.objects.all()

    return render_to_response('select_communication.html', locals(), context_instance=RequestContext(request))


@staff_member_required
def compose_mail(request, communication=None, restart=True):

    c = Communication.objects.get(id = communication)

    if restart:
        try:
            del request.session[c]
        except:
            pass

    if c in request.session:
        instances = request.session[c]
    else:
        instances = list(c.get_instances())
        request.session[c] = instances


    if request.method == 'POST' and 'sub_button' in request.POST:
        if request.POST['sub_button'] <> 'Posponer':
            form = ComposeMail(data=request.POST)
            message = form.save(commit = False)
            message.communication = c
            message.instance_object = instances[0]
            message.sent = request.POST['sub_button'] == "Aceptar"
            message.save()
            if message.sent:
                c.account.send_mail(message)
                
        del instances[0]
        request.session[c] = instances

    if len(instances) > 0:
        message = c.get_message(instances[0])
        form = ComposeMail(instance=message)
    else:
        return render_to_response('no_hay_mas_mails.html', locals(), context_instance=RequestContext(request))

    return render_to_response('compose_mail.html', locals(), context_instance=RequestContext(request))
    
